﻿<%@ WebHandler Language="C#" Class="AvatarService" %>

using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public class AvatarService : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string action = context.Request["myaction"];
        if (action == "save")
        {
            float pw = float.Parse(context.Request["pw"]);
            float ph = float.Parse(context.Request["ph"]);
            float px = float.Parse(context.Request["px"]);
            float py = float.Parse(context.Request["py"]);

            string ppath = context.Request["ppath"];
            ppath = context.Server.MapPath(ppath);
            string imgpath = string.Empty;
            Bitmap b = new Bitmap(ppath);

            string extend = Path.GetExtension(ppath).ToLower();
            //剪裁图片
            RectangleF rec = new RectangleF(px, py, pw, ph);
            Bitmap nb = b.Clone(rec, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            //从重保存图片
            ppath = ppath.Replace(extend, "") + "_sml" + extend;
            nb.Save(ppath);
            context.Response.Write("保存成功！");

        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}